//
//  ViewController.m
//  智慧小区
//
//  Created by 高永杨 on 2017/3/16.
//  Copyright © 2017年 zhongchuang. All rights reserved.
//

#import "ViewController.h"
#import "LoginVContro.h"
#import "Singleton.h"
#define loginUrl @"http://whzclf.tpddns.cn:7070/agric/api/auth/login"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageVieFull;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view layoutIfNeeded];
    NSArray* arrImages = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    CGSize screenSize = CGSizeMake(MIN(size.width, size.height), MAX(size.width, size.height));
    NSString *imageName;
    NSString *orientation = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)?@"Portrait":@"Landscape";
    for (NSDictionary* dictLaunch in arrImages) {
        size = CGSizeFromString(dictLaunch[@"UILaunchImageSize"]);
        CGSize imageSize = CGSizeMake(MIN(size.width, size.height), MAX(size.width, size.height));
        NSString *orientationLaunch = dictLaunch[@"UILaunchImageOrientation"];
        if (CGSizeEqualToSize(screenSize, imageSize) && [orientation isEqualToString:orientationLaunch]) {
            imageName = dictLaunch[@"UILaunchImageName"];
            //            NSLog(@"%@", dictLaunch);
            break;
        }
    }
    _imageVieFull.image = [UIImage imageNamed:imageName];
    // Do any additional setup after loading the view, typically from a nib.
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"login"] boolValue]) {
        //                ListViewController *list = [[ListViewController alloc]init];
        //                UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:list];
        //                self.window.rootViewController = nav;
        [self loginAgain];
        return;
    }
    LoginVContro *loginAgainVC = [[LoginVContro alloc]init];
//    Singleton *single = [Singleton sharedSimple];
//    single.loginVC = loginAgainVC;
    GYYLog(@"-----团队测试");
    [UIApplication sharedApplication].delegate.window.rootViewController = loginAgainVC;
}
-(void)loginAgain
{
    [SVProgressHUD showWithStatus:@"正在验证账号密码"];
    NSMutableDictionary *diction = [NSMutableDictionary dictionary];
    [diction setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"username"] forKey:@"loginid"];
    GYYLog(@"------密码---%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"password"]);
    AFHTTPSessionManager *manager =  [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 10.f;
    [manager POST:loginUrl parameters:diction progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        GYYLog(@"responseObject------------%@",responseObject);
        if ([responseObject[@"success"] boolValue]) {
            [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"login"];
        }
        else
        {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"账号或密码错误" message:@"请重新输入" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
            LoginVContro *logiVC = [[LoginVContro alloc]init];
            
            [UIApplication sharedApplication].delegate.window.rootViewController = logiVC;
        }
        GYYLog(@"login------%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"LOGIN------ERROR-------%@",error);
        LoginVContro *loginVErro = [[LoginVContro alloc]init];
        [SVProgressHUD showErrorWithStatus:@"请求超时,检查网络"];
        [UIApplication sharedApplication].delegate.window.rootViewController = loginVErro;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
